package com.suray.rgv.roller.core;

import com.suray.rgv60.util.RobotIOType;
import com.suray.rgv60.core.Rgv60Info;
import com.suray.type.Rgv60PalletStatus;
import com.suray.type.Rgv60TaskStatus;
import suray.rgv.communication.data.status.RobotIO;

import java.util.HashMap;
import java.util.Map;

/**
 * 滚筒车数据
 */
public class RgvRollerInfo {

    protected String serialNum;

    protected Rgv60Info rgv60Info;

    /**
     * 滚筒是否停止
     */
    protected boolean isRollerStop;

    /**
     * 滚筒状态
     */
    protected Rgv60PalletStatus palletStatus;

    public RgvRollerInfo(String serialNum, Rgv60Info rgv60Info) {
        this.serialNum = serialNum;
        this.rgv60Info = rgv60Info;
        this.palletStatus = Rgv60PalletStatus.INVALID;

        this.isRollerStop = false;
    }

    public String getSerialNum() {
        return serialNum;
    }

    /**
     *         DI_1(1, "左物料检测"),
     *         DI_2(2, "中物料检测"),
     *         DI_3(3, "右限位"),
     *         DI_4(4, "左限位"),
     *         DI_8(8, "右物料检测"),
     *         DO_5(5, "滚筒反转低速"),
     *         DO_6(6, "滚筒正转高速"),
     *         DO_7(7, "滚筒反转高速"),
     * 1,2,8检测货物，都检测到是货物到位，判定为有货
     * 否则判断为无货
     * 3,4为限位光电
     * 3个do都关闭，滚筒停止
     * 否则滚筒转动
     */
    public void analyzeRobotIO() {
        Map<RobotIOType.ROBOT_DI_TYPE, Boolean> DIs = rgv60Info.getDIs();
        Map<RobotIOType.ROBOT_DO_TYPE, Boolean> DOs = rgv60Info.getDOs();

        if (DIs.get(RobotIOType.ROBOT_DI_TYPE.DI_1) && DIs.get(RobotIOType.ROBOT_DI_TYPE.DI_2)
                && DIs.get(RobotIOType.ROBOT_DI_TYPE.DI_8)) {
            palletStatus = Rgv60PalletStatus.CAN_GET;
        } else if (!(DIs.get(RobotIOType.ROBOT_DI_TYPE.DI_1) && DIs.get(RobotIOType.ROBOT_DI_TYPE.DI_2)
                && DIs.get(RobotIOType.ROBOT_DI_TYPE.DI_8))) {
            palletStatus = Rgv60PalletStatus.CAN_PUT;
        } else {
            palletStatus = Rgv60PalletStatus.INVALID;
        }

        if (DOs.get(RobotIOType.ROBOT_DO_TYPE.DO_5) || DOs.get(RobotIOType.ROBOT_DO_TYPE.DO_6)
                || DOs.get(RobotIOType.ROBOT_DO_TYPE.DO_7)) {
            isRollerStop = false;
        } else {
            isRollerStop = true;
        }
    }

    public Rgv60TaskStatus getTaskStatus() {
        return rgv60Info.getTaskStatus();
    }
}
